package com.etang.a2020c1010;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.bizideal.smarthome.socket.SocketClient;
import com.etang.a2020c1010.tools.CodeView;
import com.etang.a2020c1010.tools.DiyToast;
import com.etang.a2020c1010.tools.MyDataBaseHelper;

public class MainActivity extends AppCompatActivity {

    private ImageView iv_code_show;
    private SharedPreferences sharedPreferences;
    private CheckBox cb_rember, cb_autologin;
    private EditText et_user, et_pass, et_yan_pass;
    private String user, pass, yan_pass;
    private MyDataBaseHelper dbHelper;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        check_isFrist();
        check_rember();
        iv_code_show.setImageBitmap(CodeView.createBitmap());
        iv_code_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_code_show.setImageBitmap(CodeView.createBitmap());
            }
        });
        cb_rember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {

                } else {
                    cb_autologin.setChecked(false);
                }
            }
        });
        cb_autologin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cb_rember.setChecked(true);
                } else {
                    cb_rember.setChecked(false);
                }
            }
        });
    }

    private void check_rember() {
        if (sharedPreferences.getBoolean("autologin", false) == true) {
            SocketClient.ip = sharedPreferences.getString("ip", null);
            startActivity(new Intent(getApplicationContext(), LoadingActivity.class));
            finish();
        }
        if (sharedPreferences.getBoolean("rember", false) == true) {
            et_pass.setText(sharedPreferences.getString("pass", null));
            et_user.setText(sharedPreferences.getString("user", null));
            cb_rember.setChecked(true);
        }
    }

    private void check_isFrist() {
        if (sharedPreferences.getBoolean("is", false) == true) {

        } else {
            sharedPreferences.edit().putBoolean("is", true).commit();
            sharedPreferences.edit().putString("ip", "18.1.10.2").commit();
            sharedPreferences.edit().putString("hum_state", "开").commit();
            sharedPreferences.edit().putString("ill_state", "开").commit();
            sharedPreferences.edit().putString("temp_state", "开").commit();
            sharedPreferences.edit().putString("per_state", "关").commit();
            sharedPreferences.edit().putString("temp_values", "30").commit();
            sharedPreferences.edit().putString("hum_values", "10").commit();
            sharedPreferences.edit().putString("ill_values_max", "200").commit();
            sharedPreferences.edit().putString("ill_values_max", "50").commit();
        }
    }

    private void initView() {
        sharedPreferences = getSharedPreferences("rember", MODE_PRIVATE);
        iv_code_show = (ImageView) findViewById(R.id.iv_code_show);
        cb_autologin = (CheckBox) findViewById(R.id.cb_autologin);
        cb_rember = (CheckBox) findViewById(R.id.cb_rember);
        et_user = (EditText) findViewById(R.id.et_user);
        et_pass = (EditText) findViewById(R.id.et_pass);
        et_yan_pass = (EditText) findViewById(R.id.et_yan_pass);
        dbHelper = new MyDataBaseHelper(getApplicationContext(), "user.db", null, 2);
        db = dbHelper.getWritableDatabase();
    }

    public void login(View view) {
        user = et_user.getText().toString();
        pass = et_pass.getText().toString();
        yan_pass = et_yan_pass.getText().toString();
        if (user.isEmpty()) {
            DiyToast.showToast(getApplicationContext(), "请输入用户名");
        } else if (pass.isEmpty()) {
            DiyToast.showToast(getApplicationContext(), "请输入密码");
        } else if (yan_pass.isEmpty()) {
            DiyToast.showToast(getApplicationContext(), "请输入验证码");
        } else {
            Cursor cursor = db.rawQuery("select * from user where username = ? and passward = ?", new String[]{user, pass});
            if (cursor.moveToNext()) {
                if (yan_pass.equals(CodeView.code)) {
                    if (cb_autologin.isChecked()) {
                        sharedPreferences.edit().putBoolean("autologin", true).putBoolean("rember", true).putString("user", user).putString("pass", pass).commit();
                    } else if (cb_rember.isChecked()) {
                        sharedPreferences.edit().putBoolean("autologin", false).putBoolean("rember", true).putString("user", user).putString("pass", pass).commit();
                    } else {
                        sharedPreferences.edit().putBoolean("autologin", false).putBoolean("rember", false).putString("user", user).putString("pass", pass).commit();
                    }
                    SocketClient.ip = sharedPreferences.getString("ip", null);
                    startActivity(new Intent(getApplicationContext(), LoadingActivity.class));
                    finish();
                } else {
                    DiyToast.showToast(getApplicationContext(), "验证码错误");
                }
            } else {
                DiyToast.showToast(getApplicationContext(), "用户名或密码错误");
            }
        }
    }

    public void ip_setting(View view) {
        View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.popwindow_showip, null, false);
        final PopupWindow popupWindow = new PopupWindow(view1, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        popupWindow.showAsDropDown(view);
        final EditText et_ip = (EditText) view1.findViewById(R.id.et_ip);
        final Button btn_con = (Button) view1.findViewById(R.id.btn_ip_con);
        et_ip.setText(sharedPreferences.getString("ip", null));
        btn_con.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_ip.getText().toString().isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "请输入IP地址");
                } else {
                    sharedPreferences.edit().putString("ip", et_ip.getText().toString()).commit();
                    popupWindow.dismiss();
                }
            }
        });
    }
}